package com.project.ToDone;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This class determines the activity that occurs within the Register
 * screen of the program.
 * 
 * @author eliselivingston
 *
 */
public class RegisterActivity extends Activity {
	
	/**
	 * This method determines the initial conditions when the register screen
	 * is first created.
	 * 
	 * @param Bundle savedInstanceState
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//set view to register.xml
		setContentView(R.layout.register);
		
		TextView loginScreen = (TextView) findViewById(R.id.link_to_login);
		
		//Listen for register button
		Button registerButton = (Button)findViewById(R.id.registerButton);
		registerButton.setOnClickListener(
				new View.OnClickListener() {
					private String username;
					private String password;
					private String password2;
					private String email;
					private String name;
					
					/**
					 * This method check to make sure that there is valid information
					 * inputted for registration.
					 * 
					 * @param View v
					 */
					//On click, checks that information is valid, and then passes it to MCP, and moves to Home page
					public void onClick(View v) {
						EditText usernameEdit = (EditText)findViewById(R.id.usernameEditText);
						EditText passwordEdit = (EditText)findViewById(R.id.passwordEditText);
						EditText password2Edit = (EditText)findViewById(R.id.password2EditText);
						EditText emailEdit = (EditText)findViewById(R.id.emailEditText);
						EditText nameEdit = (EditText)findViewById(R.id.nameEditText);
						
						TextView registerResponse = (TextView) findViewById(R.id.registrationResponse);
						
						username = usernameEdit.getText().toString();
						password = passwordEdit.getText().toString();
						password2 = password2Edit.getText().toString();
						email = emailEdit.getText().toString();
						name = nameEdit.getText().toString();
						
						RegexTester regex = new RegexTester();
						
						if(!password.equals(password2)){
							//SOME SORT OF NOTIFICATION NEEDS TO HAPPEN FOR UNMATCHING PASSWORDS
							registerResponse.setText("Passwords do not match.");
						}
						//else if(!regex.emailTest(email)){
						//	registerResponse.setText("Not a valid email.");
							
						//}
						else if(!regex.nameTest(name)){
							registerResponse.setText("Not a valid name.");
						}
						
						else{
							if(MasterControlProgram.checkIfValid(username)){
								//Moves to home page
								if(MasterControlProgram.checkIfValidPassword(password)){
									MasterControlProgram.registerUser(name, email, username, password);
									MasterControlProgram.setNewUser(true);
									
									Intent i = new Intent(getApplicationContext(), HomeActivity.class);
									startActivity(i);
								}
								else{
									registerResponse.setText("That is an invalid password");
								}
								
							}
							else{
								registerResponse.setText("Username "+ username+" already exists");
							}
						}
						
					
						
					}
				});
		
		//listen to Login screen
		loginScreen.setOnClickListener(new View.OnClickListener() {
			
			/**
			 * This method closes the registration screen.
			 * 
			 * @param View v
			 */
			public void onClick(View v) {
				// Close registration screen
				finish();
				
			}
		});
	}
}
